﻿<#-- 
 
 crea una clase ... para la tabla  ${table}...
 Author : Luis Molina...
 
 -->
<#-- variables used internally by the generator... -->
<#assign nameFile="aspx">
<#assign extensionFile="cs">
<#assign languageGenerated="c#">
<#assign description="c#">
<#assign targetDirectory="clases">
<#assign appliesToAllTables="true">
<#-- END variables used internally by the generator... -->

Partial Class listado_${table}
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        If Not Page.IsPostBack Then
            Call rellenarGrid()

        End If

    End Sub

    Private Sub rellenarGrid()
        lo.rellenarGrid("select id${table}, descripcion from ${table}", GridView1, ctes.conStringAdoGeneral, Nothing)
    End Sub

    Private Sub GridView1_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles GridView1.PageIndexChanging
        GridView1.PageIndex = e.NewPageIndex
        Call rellenarGrid()
    End Sub



    Protected Sub GridView1_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles GridView1.RowCommand

        If e.CommandArgument <> "" Then
            Dim id As Integer
            id = sf.Entero(GridView1.DataKeys(e.CommandArgument).Value)

            Select Case e.CommandName
                Case "deletex"
                    ${table}.delete(id)
                    Call rellenarGrid()

                Case "edit"
                    Response.Redirect("./detalle_${table}.aspx?id=" & id.ToString())
            End Select
        End If

    End Sub

    Private Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound

        If e.Row.RowType = DataControlRowType.DataRow Then

            Dim js As String = "confirm('Seguro que quiere eliminar ...?');"

            ' el boton delete esta en la segunda celda...
            Dim butDelete As ImageButton = CType(e.Row.Cells(1).Controls(0), ImageButton)
            If Not butDelete Is Nothing Then
                butDelete.OnClientClick() = js
            End If

            ' nota: para un linkbutton sería así...
            'Dim _singleClickButton As LinkButton = CType(e.Row.Cells(0).Controls(0), LinkButton)


        End If
    End Sub

    ' esto sirve para que no de el error cuando se utiliza itemtemplate en gridview...
    Protected Overrides Sub Render(ByVal writer As System.Web.UI.HtmlTextWriter)
        For Each row As GridViewRow In GridView1.Rows

            If row.RowType = DataControlRowType.DataRow Then
                'ClientScript.RegisterForEventValidation(CType(row.Cells(0).Controls(1), ImageButton).UniqueID, "Select$" + row.RowIndex.ToString())
                'ClientScript.RegisterForEventValidation(CType(row.Cells(0).Controls(3), ImageButton).UniqueID, "Select$" + row.RowIndex.ToString())
            End If

        Next

        MyBase.Render(writer)
    End Sub

    Protected Sub GridView1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GridView1.SelectedIndexChanged

    End Sub
End Class
